package cn.zn.modules.pz.cur.controller;

import cn.zn.framework.common.utils.FileUtils;
import cn.zn.framework.common.utils.R;
import cn.zn.modules.pz.cur.service.TpAttachmentService;
import cn.zn.modules.pz.enums.ProcessAttachTypeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;

@RestController
@RequestMapping("pz/cur/pzattach")
public class TpAttachmentController {

    @Autowired
    TpAttachmentService tpAttachmentService;
    @Autowired
    FileUtils fileUtils;

    /**
     * 查询附件列表
     *
     * @param mainId
     * @return
     */
    @GetMapping("list/{mainId}")
    public R list(@PathVariable String mainId) {
        return R.ok().put("list", tpAttachmentService.findByMainId(mainId));
    }

    /**
     * 上传附件
     *
     * @return
     */
    @PostMapping("upload/{mainId}")
    public R upload(@PathVariable String mainId, @RequestParam("fileupload") MultipartFile[] fileupload) {
        return R.ok().put("list", tpAttachmentService.saveAttach(ProcessAttachTypeEnum.GENERAL, mainId, fileupload));
    }

    /**
     * 上传借款单据
     *
     * @param mainId
     * @param fileupload
     * @return
     */
    @PostMapping("upload/loan")
    public R uploadLoan(String mainId, @RequestParam("fileupload") MultipartFile[] fileupload) {
        tpAttachmentService.saveAttach(ProcessAttachTypeEnum.LOAN, mainId, fileupload);
        return R.ok();
    }

    /**
     * 删除附件
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    public R delete(@RequestBody String[] ids) {
        tpAttachmentService.deleteAttach(Arrays.asList(ids));
        return R.ok();
    }
}
